﻿Imports System.Collections.Generic
Imports System.IO
Imports System.Text.RegularExpressions

Module modFile
    Public Enum readResult

        OK = 0
        FileNotFound = -1
        Exception = -2
    End Enum

    Public Function File_LocateDBMA() As String
        Dim _files As String() = Nothing

        _files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "Demonbuddy Modification Assistant.*",
                                    SearchOption.TopDirectoryOnly)

        If (_files.Length > 0) Then
            Return _files(0)
        End If

        Return Nothing
    End Function

    Public Function File_ReadToList(filePath As String, ByRef outList As List(Of String)) As readResult

        Dim _return As New List(Of String)
        Dim _line As String

        If File.Exists(filePath) = False Then Return readResult.FileNotFound

        Try

            Using _reader As StreamReader = New StreamReader(filePath)

                _line = _reader.ReadLine()

                While Not (_line Is Nothing)

                    _return.Add(_line)
                    _line = _reader.ReadLine()

                End While

            End Using

            outList.AddRange(_return)

            Return readResult.OK

        Catch ex As Exception

            Return readResult.Exception

        End Try
    End Function

    Public Function File_GetVersionFromAssemblyInfo(ByVal fileName As String) As Version

        Dim _list As New List(Of String)
        Dim _tmp As String

        If Path.GetDirectoryName(fileName) = String.Empty Then

            fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName)

        End If

        File_ReadToList(fileName, _list)

        For i As Integer = 0 To _list.Count - 1

            Dim _line As String = _list(i)

            If _line.Contains("Assembly: AssemblyFileVersion") Then

                _tmp = String_Between(_line, "<Assembly: AssemblyFileVersion(""", """)>")

                Return String_ConvertToVersion(_tmp)

            End If

        Next

        Return Nothing
    End Function

    Public Function String_ConvertToVersion(input As String) As Version

        Dim _text As String = input
        Dim _regex As Match = Regex.Match(_text, "(\d+).*?(\d+).*?(\d+).*?(\d+)", RegexOptions.IgnoreCase)
        Dim _major As Integer
        Dim _minor As Integer
        Dim _majorRevision As Integer
        Dim _minorRevision As Integer

        Dim _output As New Version

        If (_regex.Success) Then

            If (_regex.Groups(0).Value IsNot Nothing) Then
                _major = _regex.Groups(1).Value

            End If

            If (_regex.Groups(1).Value IsNot Nothing) = False Then
                _minor = _regex.Groups(2).Value
            End If

            If (_regex.Groups(2).Value IsNot Nothing) Then
                _majorRevision = _regex.Groups(3).Value
            End If

            If (_regex.Groups(3).Value IsNot Nothing) Then
                _minorRevision = _regex.Groups(4).Value
            End If

            Return New Version(_major, _minor, _majorRevision, _minorRevision)

        End If

        Return Nothing
    End Function
End Module